package com.uls.questionnaire.service.impl;

import java.util.List;
import com.uls.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.uls.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.uls.questionnaire.domain.ZxhOptiones;
import com.uls.questionnaire.mapper.ZxhQuestionMapper;
import com.uls.questionnaire.domain.ZxhQuestion;
import com.uls.questionnaire.service.IZxhQuestionService;

/**
 * 问题信息Service业务层处理
 * 
 * @author zxh
 * @date 2024-10-31
 */
@Service
public class ZxhQuestionServiceImpl implements IZxhQuestionService 
{
    @Autowired
    private ZxhQuestionMapper zxhQuestionMapper;

    /**
     * 查询问题信息
     * 
     * @param questionId 问题信息主键
     * @return 问题信息
     */
    @Override
    public ZxhQuestion selectZxhQuestionByQuestionId(Long questionId)
    {
        return zxhQuestionMapper.selectZxhQuestionByQuestionId(questionId);
    }

    /**
     * 查询问题信息列表
     * 
     * @param zxhQuestion 问题信息
     * @return 问题信息
     */
    @Override
    public List<ZxhQuestion> selectZxhQuestionList(ZxhQuestion zxhQuestion)
    {
        return zxhQuestionMapper.selectZxhQuestionList(zxhQuestion);
    }

    /**
     * 新增问题信息
     * 
     * @param zxhQuestion 问题信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZxhQuestion(ZxhQuestion zxhQuestion)
    {
        zxhQuestion.setCreateTime(DateUtils.getNowDate());
        int rows = zxhQuestionMapper.insertZxhQuestion(zxhQuestion);
        insertZxhOptiones(zxhQuestion);
        return rows;
    }

    /**
     * 修改问题信息
     * 
     * @param zxhQuestion 问题信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZxhQuestion(ZxhQuestion zxhQuestion)
    {
        zxhQuestionMapper.deleteZxhOptionesByQuestionId(zxhQuestion.getQuestionId());
        insertZxhOptiones(zxhQuestion);
        return zxhQuestionMapper.updateZxhQuestion(zxhQuestion);
    }

    /**
     * 批量删除问题信息
     * 
     * @param questionIds 需要删除的问题信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZxhQuestionByQuestionIds(Long[] questionIds)
    {
        zxhQuestionMapper.deleteZxhOptionesByQuestionIds(questionIds);
        return zxhQuestionMapper.deleteZxhQuestionByQuestionIds(questionIds);
    }

    /**
     * 删除问题信息信息
     * 
     * @param questionId 问题信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZxhQuestionByQuestionId(Long questionId)
    {
        zxhQuestionMapper.deleteZxhOptionesByQuestionId(questionId);
        return zxhQuestionMapper.deleteZxhQuestionByQuestionId(questionId);
    }

    /**
     * 新增选项信息信息
     * 
     * @param zxhQuestion 问题信息对象
     */
    public void insertZxhOptiones(ZxhQuestion zxhQuestion)
    {
        List<ZxhOptiones> zxhOptionesList = zxhQuestion.getZxhOptionesList();
        Long questionId = zxhQuestion.getQuestionId();
        if (StringUtils.isNotNull(zxhOptionesList))
        {
            List<ZxhOptiones> list = new ArrayList<ZxhOptiones>();
            for (ZxhOptiones zxhOptiones : zxhOptionesList)
            {
                zxhOptiones.setQuestionId(questionId);
                list.add(zxhOptiones);
            }
            if (list.size() > 0)
            {
                zxhQuestionMapper.batchZxhOptiones(list);
            }
        }
    }
}
